import storage from './storage';
import menu from './menu';
/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth(tableName, key) {
  let role = storage.get('role');
  if (!role) {
    role = '管理员';
  }
  let menus = menu.list();
  for (let i = 0; i < menus.length; i++) {
    if (menus[i].roleName == role) {
      for (let j = 0; j < menus[i].backMenu.length; j++) {
        for (let k = 0; k < menus[i].backMenu[j].child.length; k++) {
          if (tableName == menus[i].backMenu[j].child[k].tableName) {
            let buttons = menus[i].backMenu[j].child[k].buttons.join(',');
            return buttons.indexOf(key) !== -1 || false;
          }
        }
      }
    }
  }
  // for(let i=0;i<menus.length;i++){
  //     if(menus[i].roleName==role){
  //         for(let j=0;j<menus[i].backMenu.length;j++){
  //             if(menus[i].backMenu[j].tableName==tableName){
  //                 let buttons = menus[i].backMenu[j].child[0].buttons.join(',');
  //                 return buttons.indexOf(key) !== -1 || false
  //             }
  //         }
  //     }
  // }
  return false;
}

/**
 *  * 获取当前时间（yyyy-MM-dd hh:mm:ss）
 *   */
export function getCurDateTime() {
  let currentTime = new Date(),
    year = currentTime.getFullYear(),
    month =
      currentTime.getMonth() + 1 < 10
        ? '0' + (currentTime.getMonth() + 1)
        : currentTime.getMonth() + 1,
    day =
      currentTime.getDate() < 10
        ? '0' + currentTime.getDate()
        : currentTime.getDate(),
    hour = currentTime.getHours(),
    minute = currentTime.getMinutes(),
    second = currentTime.getSeconds();
  return (
    year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  );
}

/**
 *  * 获取当前日期（yyyy-MM-dd）
 *   */
export function getCurDate() {
  let currentTime = new Date(),
    year = currentTime.getFullYear(),
    month =
      currentTime.getMonth() + 1 < 10
        ? '0' + (currentTime.getMonth() + 1)
        : currentTime.getMonth() + 1,
    day =
      currentTime.getDate() < 10
        ? '0' + currentTime.getDate()
        : currentTime.getDate();
  return year + '-' + month + '-' + day;
}

export function newDate(time) {
  var date = new Date(time);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = date.getDate();
  d = d < 10 ? '0' + d : d;
  var h = date.getHours();
  h = h < 10 ? '0' + h : h;
  var minute = date.getMinutes();
  minute = minute < 10 ? '0' + minute : minute;
  var s = date.getSeconds();
  s = s < 10 ? '0' + s : s;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + s;
}
